package com.laoma.stockcenter.service.impl;

import com.laoma.ordercenter.common.CommonResponse;
import com.laoma.stockcenter.common.CommonException;
import com.laoma.stockcenter.dao.entity.StockInfo;
import com.laoma.stockcenter.dao.mapper.StockInfoMapper;
import com.laoma.stockcenter.service.StockService;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @author 老马
 * @apiNote
 * @date 2022-01-24 16:21
 **/
@Service
public class StockServiceImpl implements StockService {
    @Resource
    private StockInfoMapper stockInfoMapper;
    @Override
    public CommonResponse reduct(String productId) throws Exception {
        //数据校验
        if(StringUtils.isBlank(productId)) {
            throw CommonException.buildCommonException("没有获得产品id");
        }
        StockInfo stockInfo = this.stockInfoMapper.selectByProductId(productId);
        if(stockInfo == null) {
            throw CommonException.buildCommonException("产品不存在");
        }
        //库存-1
        this.stockInfoMapper.reductCountByProductId(productId);
        // 返回成功
        return CommonResponse.buildSuccessCommonResponse();
    }
}
